/*
 * ecx_attr.c
 * cezanne@clunix,com, 2005.12
 *
 * Copyright (c) 2001-2008 by Clunix Inc. All Rights Reserved.
 */

#include "config.h"

#include "edbd.h"
#include "edbd_ecx.h"

void
add_attr(ECXATTR_t ATTR, const char *name, const char *c_value)
{
	ecm_hashtable_t	attrs = (ecm_hashtable_t)ATTR;
	char	*value;

	if (c_value == NULL)
		value = ecm_strdup("");
	else
		value = ecm_strdup(c_value);
	if (!ecm_hashtable_str_insert(attrs, value, name)) {
		char	*old;

		if ((old = ecm_hashtable_str_replace(attrs, value, name)))
			ecm_free(old);
		else
			ecm_free(value);
	}
}

const char *
get_attr(ECXATTR_t ATTR, const char *name)
{
	ecm_hashtable_t	attrs = (ecm_hashtable_t)ATTR;

	return ecm_hashtable_str_get(attrs, name);
}

void
del_attr(ECXATTR_t ATTR, const char *name)
{
	ecm_hashtable_t	attrs = (ecm_hashtable_t)ATTR;
	char	*value;

	value = ecm_hashtable_str_remove(attrs, name);
	ecm_free(value);
}

static ecm_hashtable_t
create_attrs(ecm_hashtable_t attrs)
{
	ecm_hashtable_t	attrs_new;

	attrs_new = ecm_hashtable_create(8);

	if (attrs) {
		ecm_ht_iterator_t	iterator;
		char	*value;
		const char	*name;

		for (value = ecm_hashtable_str_first(attrs, &iterator, &name);
		     value;
		     value = ecm_hashtable_str_next(attrs, &iterator, &name))
			add_attr(attrs_new, name, value);
	}
	return attrs_new;
}

static void
free_attrs(ecm_hashtable_t attrs)
{
	ecm_ht_iterator_t	iterator;
	char	*value;

	for (value = ecm_hashtable_first(attrs, &iterator, NULL);
	     value;
	     value = ecm_hashtable_next(attrs, &iterator, NULL))
		ecm_free(value);
	ecm_hashtable_finalize(attrs);
}

ECXATTR_t
createv_attrset(ecx_t *ecx, ECXATTR_t ATTR, va_list ap)
{
	ecm_hashtable_t	attrs = (ecm_hashtable_t)ATTR;
	ecm_hashtable_t	attrs_new;
	const char	*name, *value;

	attrs_new = create_attrs(attrs);
	while (ECM_TRUE) {
		name = va_arg(ap, const char *);
		if (ecm_empty_str(name))
			break;
		value = va_arg(ap, const char *);
		add_attr(attrs_new, name, value);
	}
	ecm_ptrs_add(ecx->info->ATTRs, attrs_new);
	return attrs_new;
}

ECXATTR_t
create_attrset(ecx_t *ecx, ECXATTR_t ATTR, ...)
{
	va_list	ap;
	ECXATTR_t	RES;

	va_start(ap, ATTR);
	RES = createv_attrset(ecx, ATTR, ap);
	va_end(ap);
	return RES;
}

void
free_attrset(ecx_t *ecx, ECXATTR_t ATTR)
{
	ecm_ptrs_remove_data(ecx->info->ATTRs, ATTR);
	free_attrs(ATTR);
}

void
free_all_attrset(ecx_info_t *ecx_info)
{
	ecm_hashtable_t	attrs;
	unsigned	i;

	for (i = 0; i < ecx_info->ATTRs->len; i++) {
		attrs = ecx_info->ATTRs->pdata[i];
		if (attrs) {
			free_attrs(attrs);
			ecm_ptrs_remove(ecx_info->ATTRs, i + 1);
		}
	}
}

void
attr_print(ecx_info_t *ecx_info, ECXATTR_t ATTR)
{
	ecm_hashtable_t	attrs = (ecm_hashtable_t)ATTR;
	ecm_ht_iterator_t	iterator;
	const char	*name;
	char	*value;

	if (attrs == NULL)
		return;
	for (value = ecm_hashtable_str_first(attrs, &iterator, &name);
	     value;
	     value = ecm_hashtable_str_next(attrs, &iterator, &name)) {
		if (ecm_empty_str(value))
			fprintf(ecx_info->fp, " %s", name);
		else
			fprintf(ecx_info->fp, " %s=\"%s\"", name, value);
	}
}

sr_t *
attr_get_sr(ECXATTR_t ATTR)
{
	ecm_hashtable_t	attrs = (ecm_hashtable_t)ATTR;
	ecm_ht_iterator_t	iterator;
	const char	*name;
	char	*value;
	sr_t	*sr_attrs;

	if (attrs == NULL)
		return NULL;
	sr_attrs = sr_create_dir();
	for (value = ecm_hashtable_str_first(attrs, &iterator, &name);
	     value;
	     value = ecm_hashtable_str_next(attrs, &iterator, &name))
		sr_createc_str(sr_attrs, name, ST_STR, value);
	if (sr_get_num_child(sr_attrs) > 0)
		return sr_attrs;
	sr_free(sr_attrs);
	return NULL;
}
